package com.corner.ui.adapter;

import android.content.Context;
import android.content.Intent;
import android.net.Uri;
import android.view.View;
import android.view.ViewGroup;
import android.widget.ImageView;
import android.widget.LinearLayout;
import android.widget.TextView;

import com.corner.R;
import com.corner.app.ConsTants;

import com.corner.model.db.DBConstans;
import com.corner.model.db.Session;
import com.corner.presenter.imp.NotifyItemPresenter;
import com.corner.ui.activity.ChatActivity;
import com.corner.ui.activity.ProfileActivity;
import com.corner.ui.base.BaseMVPAdapter;
import com.corner.ui.view.INotifyItemView;
import com.corner.utils.DateFormatUtil;
import com.corner.utils.view.CustomFont;
import com.corner.utils.view.FindView;
import com.corner.utils.view.InjectViewUtils;
import com.facebook.drawee.view.SimpleDraweeView;

import java.util.List;

/**
 * Created by Administrator on 2015/5/20.
 */
public class NotifyAdapter extends BaseMVPAdapter<NotifyItemPresenter> implements View.OnClickListener, INotifyItemView {
    private List<Session> sessions;
    private Context context;

    public NotifyAdapter(List<Session> sessions, Context context) {
        this.sessions = sessions;
        this.context = context;
    }

    @Override
    public int getCount() {
        return sessions.size();
    }

    @Override
    public Object getItem(int position) {
        return sessions.get(position);
    }

    @Override
    public long getItemId(int position) {
        return position;
    }

    @Override
    public View getView(int position, View convertView, ViewGroup parent) {
        if (convertView == null) {
            convertView = View.inflate(context, R.layout.lv_notify_item, null);
            Holder holder = new Holder();
            InjectViewUtils.injectView(holder, convertView, context);
            convertView.setTag(holder);
        }
        final Holder holder = (Holder) convertView.getTag();
        final Session session = sessions.get(position);
        holder.userId = session.getSessionName();
        presenter.requestUser(holder, holder.userId);

        switch (session.getType()) {
            case DBConstans.LIKE:
                holder.tvContent.setText(session.getText());
                break;
        }
        if (!session.getIsReaded()) {
            holder.ivReaded.setVisibility(View.VISIBLE);
        } else {
            holder.ivReaded.setVisibility(View.INVISIBLE);
        }
        holder.tvTime.setText(DateFormatUtil.getShowTimeWithHmInTodayOrMdBeforeToday(session.getTimestamp().getTime(), context));
        holder.ivAvater.setOnClickListener(this);
        holder.ivAvater.setTag(session);
        holder.llContent.setOnClickListener(this);
        holder.llContent.setTag(session);
        return convertView;
    }


    @Override
    public void onClick(View v) {
        switch (v.getId()) {
            case R.id.iv_notify_avater:
                Intent intent = new Intent(context, ProfileActivity.class);
                Session session = (Session) v.getTag();
                intent.putExtra(ProfileActivity.USER_ID, Long.parseLong(session.getSessionName()));
                context.startActivity(intent);
                break;
            case R.id.ll_notify_content:
                intent = new Intent(context, ChatActivity.class);
                intent.putExtra(ChatActivity.INTENT_ID, ChatActivity.CHAT);
                session = (Session) v.getTag();
                intent.putExtra(ChatActivity.USER_ID_IN_LONG,Long.parseLong(session.getSessionName()));
                context.startActivity(intent);
                break;
        }
    }

    @Override
    protected NotifyItemPresenter createP() {
        return new NotifyItemPresenter();
    }

    @Override
    public void setAvaterUri(Holder holder, String userId, Uri uri) {
        if (holder.userId.equals(userId)) {
            holder.ivAvater.setImageURI(uri);
        }
    }

    public void changeList(List<Session> sessions) {
        this.sessions = sessions;
        notifyDataSetChanged();
    }

    public class Holder {
        @FindView(R.id.iv_notify_readed)
        public ImageView ivReaded;
        @FindView(R.id.iv_notify_avater)
        public SimpleDraweeView ivAvater;
        @FindView(R.id.ll_notify_content)
        public LinearLayout llContent;

        @CustomFont(ConsTants.TYPE_FACE)
        @FindView(R.id.tv_notify_content)
        public TextView tvContent;

        @CustomFont(ConsTants.TYPE_FACE)
        @FindView(R.id.tv_notify_time)
        public TextView tvTime;

        private String userId;
    }
}
